# -*- coding: utf-8 -*-
# @Time    : 2020/3/12 下午6:20
# @Author  : lilong

"""
通过定义装饰器来定义单例：
   多线程时要注意
"""


def singletonDecorator(cls, *args, **kwargs):
    """定义一个单例装饰器"""

    instance = {}

    def wrapperSingleton(*args, **kwargs):
        if cls not in instance:
            instance[cls] = cls(*args, **kwargs)
        return instance[cls]

    return wrapperSingleton


@singletonDecorator
class Singleton3:
    """使用单例装饰器修饰一个类"""

    def __init__(self, name):
        self.__name = name

    def getName(self):
        return self.__name


tony = Singleton3("Tony")
karry = Singleton3("Karry")
print(tony.getName(), karry.getName())
print("id(tony):", id(tony), "id(karry):", id(karry))
print("tony == karry:", tony == karry)

